<?php
class ListdocumentForm extends Form
{
	function __construct()
	{
		Form::Form('ListdocumentForm');
	}

	function draw()
	{
		global $display;
		$this->beginForm(false,"POST",false,Url::build_current());
		
		$key_word 	= Url::get('key_word');
		$condition	=	" status=1";
			
		$display->add('key_word', $key_word);
		
		if($key_word)
		{
			$key_word_s = '';
			
			$arr = explode(' ',$key_word);
			
			if($arr)
			{
				foreach ($arr as $key)
				{
					$key = trim($key);
					
					if($key != '')
					{
						$key_word_s .= ($key_word_s==''?'+':' +')."$key";
					}
				}
			}
			
			if($key_word_s != '')
			{
				$condition .=" AND MATCH(title, des) AGAINST ('$key_word_s' IN BOOLEAN MODE)";
			}
			
			//$condition .= ' AND (title LIKE "%'.$key_word.'%" OR des LIKE "%'.$key_word.'%")';
		}
		
		$total_item	=	DB::count('document',$condition);
			
		$limit='';
		$paging	=	AZPagging::paging_list($limit,$total_item,10,10,'page_no','',true,'Tài liệu','Trang');
		
		$display->add('paging',$paging);
		
		$condition = 'WHERE'.$condition;
		
		$documents	=	array();
		
		$sql	=	'SELECT * FROM document '.$condition.' ORDER BY id DESC '.$limit;
		$re 	=	DB::query($sql);
		
		if($re)
		{
			$i=1;
			while ($row = mysql_fetch_assoc($re))
			{
				$row['title'] 	= stripslashes($row['title']);
				
				$row['des'] 	= AZLib::remove_4_js(AZLib::plainText(html_entity_decode($row['des'], ENT_QUOTES, "UTF-8")));
				$row['des']  	= str_replace(array("'","\""), array(""," "),$row['des']);				
				$row['des'] 	= AZLib::word_limit($row['des'],200,'');
				
				if(++$i%2)
				{
					$row['bgcolor'] = "#FFFFFF";
				}
				else
				{
					$row['bgcolor'] = "#D9D9D9";
				}

					
				$row['time_m'] = $row['time_m']?date("d/m/Y",$row['time_m']):'';
				
				if(User::is_admin())
				{
					$row['change'] 	= AZLib::button(Url::build_current(array('cmd'=>'change','id'=>$row['id'],"href"=>urlencode(Url::build_all()))),'style/images/unexam.gif','Disable');
					
					$row['edit'] 	= AZLib::button(Url::build('document',array('cmd'=>'edit','id'=>$row['id'],"href"=>urlencode(Url::build_all()))),'style/images/edit.gif','Sửa tài liệu');
					
					$row['delete'] 	= AZLib::buttonDel(Url::build_current(array('cmd'=>'delete','id'=>$row['id'],"href"=>urlencode('?'.$_SERVER['QUERY_STRING']))),'style/images/delete.gif','Sửa tài liệu');
				}
				
				if($row['url'])
				{
					$row['document_detail'] 	= $row['url'] ;
				}
				else
				{
					$row['document_detail'] 	= WEB_DIR."download-{$row['id']}/".AZLib::safe_title($row['title']) . ".html";
				}
				
				$documents[$row['id']] = $row;
			}
		}
		
		$display->add('documents',$documents);
		
		if(User::is_admin())
		{
			$display->add("is_admin",1);
		}
		
		$display->output('DocumentList');
		$this->endForm();
	}
}

class Deletedocument extends Form
{
	function __construct()
	{
		$id = (int)Url::get('id',0);
		
		if($id && User::is_admin())
		{
			
			$document=DB::select('document',"id=$id");
			
			if($document)
			{
				$file_src = DATA_PATH."document/{$document['file_path']}";
				
				if(file_exists(ROOT_PATH.$file_src))
				{
					@unlink(ROOT_PATH.$file_src);
				}
				
				DB::delete('document',"id=$id");
			}
		}
		
		$href=urldecode(Url::get('href'));
		
		if($href)
		{
			Url::redirect_url($href);
		}
		else
		{
			Url::redirect_current();
		}
	}
}

class PublicdocumentForm extends Form
{
	function __construct()
	{
		$id	=	(int)Url::get('id',0);
		
		if($id && User::is_admin())
		{
			$document=DB::select('document',"id=$id");
			
			if($document)
			{
				DB::update('document', array('status'=>(int)(!$document['status']),'time_m'=>TIME_NOW,'user_m'=>User::user_name()),'id='.$id);
			}
			
		}
		
		if(Url::get('href'))
			Url::redirect_url(urldecode(Url::get('href')));
		else
			Url::redirect_current();
	}
}
?>